﻿//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls.DataVisualization;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;

/// <summary>
/// Extends Chart to include chart types.
/// </summary>
public class TridentChart : Chart
{
    /// <summary>
    /// Gets or sets the collection of types in this chart.
    /// </summary>
    public ObservableCollection<string> ChartTypes { get; set; }

    /// <summary>
    /// Initialize the chart.
    /// </summary>
    public TridentChart()
    {
        this.ChartTypes = new ObservableCollection<string>();
        Style legendStyle = new Style(typeof(Legend));
        legendStyle.Setters.Add(new Setter(Legend.WidthProperty, 0.0));
        this.LegendStyle = legendStyle;
        this.Loaded += new RoutedEventHandler(this.OnLoaded);
    }

    /// <summary>
    /// Specify the chart type.
    /// </summary>
    /// <param name="chartType">
    /// The type of chart to be shown.
    /// </param>
    public void ShowSeries(string chartType)
    {
        bool seriesFound = false;
        foreach (ISeries series in this.Series)
        {
            if (series.GetType().Name.Equals(chartType, StringComparison.OrdinalIgnoreCase))
            {
                (series as Series).Visibility = Visibility.Visible;
                seriesFound = true;
            }
            else
            {
                (series as Series).Visibility = Visibility.Collapsed;
            }
        }

        if (!seriesFound)
        {
            (this.Series.First(s => s.GetType().Name.Equals("ColumnSeries")) as Series).Visibility = Visibility.Visible;
        }
    }

    /// <summary>
    /// Add the types of charts supported.
    /// </summary>
    /// <param name="series">
    /// The chart type (extending data point series).
    /// </param>
    /// <param name="dependentValuePath">
    /// The data that is to be compared and represented.
    /// </param>
    /// <param name="independentValuePath">
    /// The independent data.
    /// </param>
    public void AddSeries(DataPointSeries series, string dependentValuePath, string independentValuePath)
    {
        if (series != null && !this.Series.Contains(series))
        {
            series.SetBinding(PieSeries.ItemsSourceProperty, new Binding());
            series.DependentValuePath = dependentValuePath;
            series.IndependentValuePath = independentValuePath;
            series.IsSelectionEnabled = true;
            this.Series.Add(series);
            this.ChartTypes.Add(series.GetType().Name);
        }
    }

    /// <summary>
    /// Handles on load functionality.
    /// </summary>
    /// <param name="sender">
    /// This.
    /// </param>
    /// <param name="e">
    /// RoutedEventArgs.
    /// </param>
    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        foreach (object legend in this.LegendItems)
        {
            (legend as LegendItem).Visibility = Visibility.Collapsed;
        }
    }
}